package com.cskaoyan.service;

import com.cskaoyan.bean.MarketAddress;
import com.cskaoyan.bean.MarketAddressExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class AdminAddressServiceImpl implements AdminAddressService {
    @Override
    public CommonData<MarketAddress> list(Integer userId, String name, Integer page, Integer limit, String sort, String order) {
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        if (userId!=null){

            criteria.andUserIdEqualTo(userId);
        }
        if (name!=null){

            criteria.andNameLike("%"+name+"%");

        }
   if (StringUtil.isNotEmpty(sort)&& StringUtil.isNotEmpty(order)){


       marketAddressExample.setOrderByClause(sort + " " + order);

   }

        SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        MarketAddressMapper mapper = sqlSession.getMapper(MarketAddressMapper.class);
        PageHelper.startPage(page, limit);
        List <MarketAddress> marketAddresses = mapper.selectByExample(marketAddressExample);

        if (sqlSession!=null){


            sqlSession.close();
        }



        return CommonData.data(new PageInfo(marketAddresses));





    }
}
